// package com.wing.member.controller.app;
//
// import cn.hutool.core.util.StrUtil;
// import com.alibaba.fastjson.JSONObject;
// import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
// import com.wing.common.utils.ChuangLanSmsUtil;
// import com.wing.common.utils.JsonResult;
// import com.wing.common.utils.JwtUtils;
// import com.wing.common.utils.LocaleMessage;
// import com.wing.common.utils.mail.MailHelperImpl;
// import com.wing.common.utils.redis.RedisHelperImpl;
// import com.wing.member.model.entity.Member;
// import com.wing.member.model.request.RegisterForm;
// import com.wing.member.service.MemberAuthService;
// import com.wing.member.service.MemberService;
// import io.swagger.annotations.Api;
// import io.swagger.annotations.ApiOperation;
// import org.apache.commons.lang.StringUtils;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.validation.annotation.Validated;
// import org.springframework.web.bind.annotation.PostMapping;
// import org.springframework.web.bind.annotation.RequestMapping;
// import org.springframework.web.bind.annotation.RestController;
//
// import javax.servlet.http.HttpServletRequest;
// import javax.validation.constraints.Email;
// import javax.validation.constraints.NotBlank;
// import java.io.UnsupportedEncodingException;
// import java.util.concurrent.TimeUnit;
//
// /**
//  *
//  * @author
//  */
// @Validated
// @Api(value = "App-登录注册",tags = {"登录注册"})
// @RestController(value = "appLoginController")
// @RequestMapping("app/login")
// public class LoginController {
//
//     @Autowired
//     private MemberService memberService;
// 	@Autowired
// 	private MemberAuthService memberAuthService;
// 	@Autowired
// 	private JwtUtils jwtUtils;
//     @Autowired
//     private MailHelperImpl mailHelper;
//     @Autowired
//     private RedisHelperImpl redisHelper;
//     @Autowired
//     private LocaleMessage localeMessage;
//
//
//     @ApiOperation(value = "发送邮箱验证码", notes = "发送邮箱验证码")
//     @PostMapping("/registerSendMailCaptcha")
//     public JsonResult registerSendMailCaptcha(@NotBlank(message = "{member.login.email.error}") @Email(message = "{member.login.email.error}") String email) {
//
//         //校验重复发送
//         if(redisHelper.getValue(email + "Time") != null){
//             Long curSeconds = System.currentTimeMillis() / 1000;
//             Long sendOutCodeSeconds = Long.parseLong(redisHelper.getValue(email + "Time").toString());
//             if( null != sendOutCodeSeconds && null != curSeconds && curSeconds - sendOutCodeSeconds <= 60){
//                 return JsonResult.fail(localeMessage.getMessage("member.login.captcha.error.once"));
//             }
//         }
//
//         //生成六位随机数
//         Integer captcha = (int)((Math.random()*9+1) * 100000);
//         //TODO 邮箱格式要封装
//         StringBuilder sb = new StringBuilder();
//         sb.append("Dear WING Platform User: ")
//                 .append(email)
//                 .append(", Hello: ")
//                 .append("Verification code: ")
//                 .append(captcha)
//                 .append("<br />Warm Tip: If not my own operation, please pay attention to account security");
//         String subject = "FROM WING ";
//         mailHelper.sendHtmlEmail(StringUtils.deleteWhitespace(email), subject, sb.toString());
//         redisHelper.valuePut(email, captcha);
//         redisHelper.expire(email, 15, TimeUnit.MINUTES);
//         redisHelper.valuePut(email + "Time", System.currentTimeMillis()/1000);
//
//         return JsonResult.success(localeMessage.getMessage("member.login.send.success"));
//     }
//
//
//     @ApiOperation(value = "用户注册", notes = "用户注册")
//     @PostMapping("/register")
//     public JsonResult register(@Validated(RegisterForm.Register.class) RegisterForm registerForm) {
//         //注册
//         if(memberService.register(registerForm) > 0){
//             redisHelper.remove(registerForm.getIdentifier());
//             return JsonResult.success(localeMessage.getMessage("member.login.register.success"));
//         }
//         return JsonResult.fail(localeMessage.getMessage("member.login.register.fail"));
//     }
//
//
// 	@ApiOperation(value = "登录", notes = "用户登录，登录成功返回token")
// 	@PostMapping("/login")
// 	public JsonResult login(@Validated(RegisterForm.Login.class) RegisterForm registerForm, HttpServletRequest request) throws UnsupportedEncodingException {
//
// 		// 用户登录
//         registerForm.setIpAddress(request.getRemoteAddr());
// 		Member member = memberAuthService.login(registerForm);
//
// 		// 生成token
// 		String token = jwtUtils.generateToken(member.getId());
//
//         JSONObject data = new JSONObject();
//         JSONObject memberJson = new JSONObject();
//         memberJson.put("id", member.getId());
//         memberJson.put("username", member.getUsername());
//         memberJson.put("email", StrUtil.isNotEmpty(member.getEmail()) ? member.getEmail() : "");
//         memberJson.put("phone", StrUtil.isNotEmpty(member.getPhone()) ? member.getPhone() : "");
//         memberJson.put("hasPayPassword", StrUtil.isNotEmpty(member.getPayPassword()) ? 1 : 0);
//         memberJson.put("nickname", member.getNickname());
//         memberJson.put("inviteCode", member.getInviteCode());
//         memberJson.put("headImg", member.getHeadImg());
//         memberJson.put("sex", member.getSex() != null ? member.getSex().getDesc() : "");
//         memberJson.put("signature", member.getSignature());
//         data.put("member", memberJson);
//
//         JSONObject tokenJson = new JSONObject();
//         tokenJson.put("token", token);
//         tokenJson.put("expire", jwtUtils.getExpire());
//         data.put("token", tokenJson);
//
//         return JsonResult.success(localeMessage.getMessage("member.login.success"), data);
// 	}
//
//
//     @ApiOperation(value = "发送手机验证码", notes = "发送手机验证码")
//     @PostMapping("/forgotPasswordSendPhoneCaptcha")
//     public JsonResult forgotPasswordSendPhoneCaptcha(@NotBlank(message = "{member.login.phone.error}") String phone) {
//
//         //校验重复发送
//         if(redisHelper.getValue(phone + "Time") != null){
//             Long curSeconds = System.currentTimeMillis() / 1000;
//             Long sendOutCodeSeconds = Long.parseLong(redisHelper.getValue(phone + "Time").toString());
//             if( null != sendOutCodeSeconds && null != curSeconds && curSeconds - sendOutCodeSeconds <= 60){
//                 return JsonResult.fail(localeMessage.getMessage("member.login.captcha.error.once"));
//             }
//         }
//
//         //校验手机号是否已注册
//         Member member = memberService.getOne(new QueryWrapper<Member>().eq("phone", phone));
//         if(member == null){
//             return JsonResult.fail(localeMessage.getMessage("member.login.phone.notExist"));
//         }
//
//         //生成六位随机数
//         Integer captcha = (int)((Math.random()*9+1) * 100000);
//         String msg = localeMessage.getMessage("member.login.phone.message", new String[]{captcha.toString()});
//         if(ChuangLanSmsUtil.smsSend(phone, msg)){
//             redisHelper.valuePut(phone, captcha);
//             redisHelper.expire(phone, 15, TimeUnit.MINUTES);
//             redisHelper.valuePut(phone + "Time", System.currentTimeMillis()/1000L);
//             return JsonResult.success(localeMessage.getMessage("member.login.send.success"));
//         }
//         return JsonResult.fail(localeMessage.getMessage("member.login.send.fail"));
//     }
//
//
//     @ApiOperation(value = "忘记密码发送邮箱验证码", notes = "忘记密码发送邮箱验证码")
//     @PostMapping("/forgotPasswordSendMailCaptcha")
//     public JsonResult forgotPasswordSendMailCaptcha(@NotBlank(message = "{member.login.email.error}") @Email(message = "{member.login.email.error}") String email, @NotBlank(message = "{member.login.email.error}") String username) {
//
//         //校验重复发送
//         if(redisHelper.getValue(email + "Time") != null){
//             Long curSeconds = System.currentTimeMillis() / 1000;
//             Long sendOutCodeSeconds = Long.parseLong(redisHelper.getValue(email + "Time").toString());
//             if( null != sendOutCodeSeconds && null != curSeconds && curSeconds - sendOutCodeSeconds <= 60){
//                 return JsonResult.fail(localeMessage.getMessage("member.login.captcha.error.once"));
//             }
//         }
//
//         //校验手机号是否已注册
//         Member member = memberService.getOne(new QueryWrapper<Member>().eq("username", username).eq("email", email));
//         if(member == null){
//             return JsonResult.fail(localeMessage.getMessage("member.login.username.notExist"));
//         }
//
//         //生成六位随机数
//         Integer captcha = (int)((Math.random()*9+1) * 100000);
//         //TODO 邮箱格式要封装
//         StringBuilder sb = new StringBuilder();
//         sb.append("Dear WING Platform User: ")
//                 .append(email)
//                 .append(", Hello: ")
//                 .append("Verification code: ")
//                 .append(captcha)
//                 .append("<br />Warm Tip: If not my own operation, please pay attention to account security");
//         String subject = "FROM WING ";
//         mailHelper.sendHtmlEmail(StringUtils.deleteWhitespace(email), subject, sb.toString());
//         redisHelper.valuePut(username, captcha);
//         redisHelper.expire(username, 15, TimeUnit.MINUTES);
//         redisHelper.valuePut(username + "Time", System.currentTimeMillis()/1000);
//
//         return JsonResult.success(localeMessage.getMessage("member.login.send.success"));
//     }
//
//
//     @ApiOperation(value = "忘记密码", notes = "忘记密码")
//     @PostMapping("/forgotPassword")
//     public JsonResult forgotPassword(@Validated(RegisterForm.ForgotPassword.class) RegisterForm registerForm) {
//
//         if(memberService.forgotPassword(registerForm) > 0){
//             redisHelper.remove(registerForm.getIdentifier());
//             return JsonResult.success(localeMessage.getMessage("common.success"));
//         }
//
//         return JsonResult.fail(localeMessage.getMessage("common.fail"));
//     }
//
// }
